package com.taxigang.client.micro;

import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.RunAsyncCallback;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.i18n.client.LocaleInfo;
import com.google.gwt.jquery.mobile.ui.util.JQueryMobile;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.RootPanel;
import com.taxigang.client.Session;
import com.taxigang.client.TaxiGangHandler;
import com.taxigang.client.TaxiGangMain;
import com.taxigang.client.TaxiGangProcessor;
import com.taxigang.client.TaxiGangRouteLoader;
import com.taxigang.client.TaxigangMode;
import com.taxigang.client.bookmark_bubble.BubbleFactory;
import com.taxigang.client.uibinder.Alert;
import com.taxigang.client.uibinder.TaxiGangHome;
import com.taxigang.client.uibinder_mobile.TaxiGangMobile;
/**
 * TODO
 * Remove following widgets PCSearchWidget
 * @author hall
 *
 */
public class TaxiGangCreatorPC {
	//	private final LoginServiceAsync loginService = GWT.create(LoginService.class);
	//	private final TaxiGangServiceAsync taxigangService = GWT.create(TaxiGangService.class);

	//private SearchView searchView;
	//private NavigatableMapWidget navigatableMapView;
	//private PCMapWidget pcMapWidget;
	//private HomeWidget homeWidget;
	//	private FeedbackView feedbackView;
	//	private TaxiGangHandler taxiGangHandler;
	//
	//	private NoTaxiGangsWidget noTaxiGangsWidget;
	//	private NoMyTaxiGangsWidget noMyTaxiGangsWidget;
	//	//private DialogWidget welcomeDialog;
	//	private DialogWidget congratulationsJoinDialog;
	//	private DialogWidget congratulationsCreateDialog;
	//	private DialogWidget sorryNoLeaveDialog;
	//
	//	private SuggestionWidget suggestionWidget;
	//	private MyTaxiGangsWidget myTaxiGangsWidget;
	//	private MapPresenter mapPresenter = MapPresenter.getInstance();
	//	//private HomePresenter homePresenter;
	//	private CreateView createView;
	//	private CreatePresenter createPresenter;
	//	private MyTaxiGangsPresenter myTaxiGangsPresenter;
	//	private MessagePresenter messagePresenter;
	//	private AdminPresenter adminPresenter;
	//	private FeedbackPresenter feedbackPresenter;
	//	//private WelcomePresenter welcomePresenter;
	//	//private PushButton fbLoginButton=new PushButton();
	//
	//	private SearchView searchView=null;



	//	private void initAdmin(Session session, LayoutPanel p ){
	//		AdminView adminView = new AdminBar();
	//		AdminPresenter adminPresenter = new AdminPresenter(adminView);
	//		WelcomeView welcomeView = new WelcomePanel(p);
	//		LoginView loginView = new LoginPanel(p);
	//
	//		welcomePresenter = new WelcomePresenter(welcomeView, loginView ,adminView);		
	//		taxiGangHandler.setPresenter(welcomePresenter,adminPresenter);
	//
	//	}
	public void initUserMobile(){
		JQueryMobile.init();

		TaxiGangMobile taxiGangMobile = new TaxiGangMobile();
		RootPanel.get().add(taxiGangMobile);
		if (Session.getInstance().getRef()!=null){
			TaxiGangHandler.getInstance().showInvitation();
		}
		if (Session.getInstance().getWishRouteCancel()!=null){
			TaxiGangHandler.getInstance().showWishRouteCancel();
		}
		TaxiGangMain.hideLoading();
		//new Showcase().onModuleLoad();
		TaxiGangProcessor.authenticate();

		GWT.runAsync(new RunAsyncCallback() {
			public void onFailure(Throwable caught) {
				Window.alert("Code download failed");
			}

			public void onSuccess() {
				BubbleFactory factory = new BubbleFactory();
				factory.createBubble();

			}
		});

	}


	public void initUser(){
		final TaxiGangRouteLoader loader = new TaxiGangRouteLoader();
		TaxigangMode taxigangMode = Session.getInstance().getMode();
		
		if (TaxigangMode.WISHROUTECANCEL == taxigangMode){
			loader.cancelWishRoute();

		}else if (TaxigangMode.TAXIGANG == taxigangMode){
			loader.showInviation();

		} else {
			if (TaxigangMode.TAXIGANG != taxigangMode && TaxigangMode.WISHROUTECANCEL != taxigangMode){
				final TaxiGangHome taxiGangHome = new TaxiGangHome();
				loader.setTaxiGangHome(taxiGangHome);

				//			TaxiGangRoute mapView = new TaxiGangRoute();
				//			TaxiGangPresenter taxiGangPresenter = TaxiGangPresenter.getInstance();
				//			taxiGangPresenter.setMapView(mapView);

				HasClickHandlers showMeMoreButton = taxiGangHome.getShowMeMoreButton();

				showMeMoreButton.addClickHandler(new ClickHandler() {

					@Override
					public void onClick(ClickEvent event) {

						loader.execute();

					}
				});
				//RootPanel.get().add(mapView);
				RootPanel.get().add(taxiGangHome);

				TaxiGangMain.hideLoading();

				boolean promote = TaxigangMode.PARTNER == taxigangMode;
				
				if ("de".equals(LocaleInfo.getCurrentLocale().getLocaleName()) && promote){
					String[] message = new String[7];
//					Date date = ne
//					Calendar calendar = Calendar.getInstance();
//					int  dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
//					if (dayOfWeek <=5 ){
//						calendar.add(Calendar.DAY_OF_MONTH, 6 - dayOfWeek);
//					} else if (dayOfWeek == 7){
//						calendar.add(Calendar.DAY_OF_MONTH, 4);
//					}
//					Date date = calendar.getTime();
//					SimpleDateFormat sdf = new SimpleDateFormat("EEEEE, dd. MMMMM");

					message[0] = TaxiGangCreator.taxiGangConstants.promotionText1()+ "  Freitag, 5. April 2013";
					message[1] = TaxiGangCreator.taxiGangConstants.promotionText2();
					message[2] = TaxiGangCreator.taxiGangConstants.promotionText3();
					message[3] = TaxiGangCreator.taxiGangConstants.promotionText4();
					message[4] = TaxiGangCreator.taxiGangConstants.promotionText5();
					message[5] = TaxiGangCreator.taxiGangConstants.promotionText6();
//					message[6] = TaxiGangCreator.taxiGangConstants.promotionText7();

					Alert.promotion(null,"Taxigang",message,false,"400px");
				}

			} 
		}

	}





}
